<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

use DB;

class ShopController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        // dd($request -> all());
        // $data = DB::table('t_goods') -> get();


        $data = DB::table('t_goods as g1') -> RightJoin('t_class as c1', 'g1.type_id', '=', 'c1.id')
                                    -> RightJoin('t_stock as s1', 'g1.gid', '=', 's1.gid')
                                    -> select('g1.*',DB::raw("concat(c1.path,',',c1.id) as path_str"), 'c1.tname as ppname','s1.count as count','s1.total as total')
                                    -> orderBy('path_str')
                                    ->where('gname', 'like', '%'.$request -> keywords.'%') ->whereIn('state',[1]) -> paginate($request -> input('num', 5));
        // dd($data -> all());
        // $res = $data -> all();
        // // dd($res);
        // $num = count($res);
        // // dd($num);
        // $gid =[];
        // // var_dump($gid);
        // for ($i = 0; $i < $num ; $i++) { 
        //     $gid[$i] = $res[$i] -> gid;
        // }
        // // var_dump($gid);
        // $cnum = count($gid);
        // $kc = [];
        // for ($j=0; $j < $cnum; $j++) { 
        //     $kc[] = DB::table('t_stock') -> where('gid', $gid[$j]) -> get();
        // }
        // var_dump($kc);
        // $data = DB::table('t_stock') -> where('gid', $data)
        return view('admin.shop.indexShop', ['title' => '商品展示', 'data' => $data,'request' => $request -> all()]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        // $data = DB::table('t_class') -> orderBy('path') -> get();
        $data = DB::table('t_class as c1') ->select('*',DB::raw("concat(c1.path,',',c1.id) as s_path"))
         ->orderBy('s_path') -> get();
        // dd($data);
        return view('admin.shop.addShop',['title' => '商品添加', 'data' => $data]);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        // dd($request ->all());
        $this -> validate($request, [
            'gname' => 'required',
            'gprice' => 'required',
            'price' => 'required',
            'total' => 'required'
            ],[
            'gname.required' => '商品名不能为空',
            'gprice.required' => '售价不能为空',
            'price.required' => '进价不能为空',
            'total.required' => '商品总量不能为空',
            ]);

        $data = $request -> except('_token');
        //向t_goods内添加的
        $t_goods = $request -> except('_token','price','total');
        
        // $t_goods['home'] = $data['home'];
        // dd($t_goods['home']);
        //向t_class内添加的
        $t_stock = $request -> except('_token','type_id','gname','gprice','home');
        // dd($t_stock);
       
        //添加
        $id = DB::table('t_goods')->insertGetId($t_goods);
        // var_dump($id);die(); 
        if($id)
        {   $t_stock['gid'] = $id;
            $resC = DB::table('t_stock') -> insert($t_stock);
            if($resC)
            {
                return redirect('/admin/shop') -> with(['info' => '添加成功']);
            }else
            {
                return back() -> with(['info' => '添加失败']);
            }
        }else
        {
            return back() -> with(['info' => '添加失败']);
        }
              
        
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        // dd($id);
        $data = DB::table('t_goods') -> where('gid', $id) -> get();
        $type_id = $data[0] -> type_id;
        $gid = $data[0] -> gid;
        $gclass = DB::table('t_class') -> where('id', $type_id) -> get();

        $stock = DB::table('t_stock') -> where('gid', $gid) -> get();
        // dd($stock);
        return view('admin.shop.edit', ['title' =>'商品编辑', 'id' => $id ,'data' => $data, 'gclass' => $gclass, 'stock' => $stock]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        // return $id;
        // dd($request -> all());
        $vStock = $request -> only('price', 'total');
        // dd($vStock);
        $vGoods = $request -> only('gname', 'gprice');
        // dd($vGoods);
        $uStock = DB::table('t_stock') -> where('gid', $id) -> update($vStock);
        $uGoods = DB::table('t_goods') -> where('gid', $id) -> update($vGoods);
            if($uGoods || $uStock)
            {
                return redirect('admin/shop');
            }else
            {
                return back();
            }
        
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($gid)
    {
        // dd($gid);
        $res = DB::table('t_goods') -> where('gid', '=', $gid) -> delete();
        if($res)
        {
             return redirect('admin/shop');
        }else
        {
             return back();
        }

    }

    public function status($gid)
    {
        $state = DB::table('t_goods') -> where('gid', $gid) -> select('state') -> get();
        // var_dump($state[0] -> state);die();
        $state = $state[0] -> state;
        if($state == 1)
        {
            $res = DB::table('t_goods') ->where('gid', $gid) -> update(['state' => 0]);
            if($res)
            {
                return redirect('admin/shop/out'); 
           }else
           {
                return back();
           }

        }else if($state == 0)
        {
            $res = DB::table('t_goods') ->where('gid', $gid) -> update(['state' => 1]);
            if($res)
            {
                return redirect('admin/shop'); 
           }else
           {
                return back();
           }
        }

        

    }

    public function out(Request $request)
    {
        $data = DB::table('t_goods as g1') -> RightJoin('t_class as c1', 'g1.type_id', '=', 'c1.id')
                                    -> RightJoin('t_stock as s1', 'g1.gid', '=', 's1.gid')
                                    -> select('g1.*',DB::raw("concat(c1.path,',',c1.id) as path_str"), 'c1.tname as ppname','s1.count as count','s1.total as total')
                                    -> orderBy('path_str')
                                    ->where('gname', 'like', '%'.$request -> keywords.'%') ->whereIn('state',[0]) -> paginate($request -> input('num', 5));
       
        return view('admin.shop.out',['title' => '下架列表', 'data' => $data, 'request' => $request -> all()]);
    }

    public function doImgs($gid)
    {
        $gname = DB::table('t_goods') -> where('gid', $gid) -> value('gname');
        $photo = DB::table('t_photo') -> where('gid', $gid) -> get();
        // dd($photo);
        return view('admin.shop.addPhoto',['title' => '图片添加','gid' => $gid, 'gname' => $gname, 'photo'=> $photo]);
    }

    public function imgs(Request $request,$gid)
    {
        // dd($request -> all());
        $data = $request -> except('_token');
        $data['gid'] = $gid;
        // dd($data);
        //处理上传图片
        if($request -> hasFile('photo'))
        {
            if($request -> file('photo') -> isValid())
            {
                $suffix = $request -> file('photo') -> getClientOriginalExtension();
                $fileName = time().mt_rand(10000,99999).'.'.$suffix;
                //移动文件
                $move = $request -> file('photo') -> move('./uploads/gPhoto', $fileName);
            }

                if($move)
                {
                    $data['photo'] = $fileName;
                }else
                {
                    $data['photo'] = 'default.jpg';
                }
        }else
        {
            $data['photo'] = 'default.jpg';
        }


        $res = DB::table('t_photo') -> insert($data);
        if($res)
        {
            return redirect('admin/shop');
        }else
        {
            return back();
        }
    }

    public function ajaxDelete(Request $request)
    {
        // dd($request ->all());
        $id = $request -> id;
        $data = DB::table('t_photo') -> delete($id);
        if($data)
        {
            return 1;
        }else
        {
            return 0;
        }
    }
}
